package com.zhang.apipassenger.service;

import com.zhang.apipassenger.remote.ServicePassengerUserClient;
import com.zhang.apipassenger.remote.ServiceVerificationCodeClient;
import com.zhang.internalcommon.dto.VerificationCodeDTO;
import com.zhang.internalcommon.constant.CommonStatusEnum;
import com.zhang.internalcommon.constant.IdentityConstant;
import com.zhang.internalcommon.constant.TokenConstants;
import com.zhang.internalcommon.dto.ResponseResult;
import com.zhang.internalcommon.response.NumberCodeResponse;
import com.zhang.internalcommon.response.TokenResponse;
import com.zhang.internalcommon.utils.JwtUtils;
import com.zhang.internalcommon.utils.RedisPrefixUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author ADMIN
 */
@Service
public class VerificationCodeService {
	@Autowired
	private ServiceVerificationCodeClient serviceVerificationCodeClient;

	@Autowired
	private StringRedisTemplate redisTemplate;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private ServicePassengerUserClient passengerUserClient;

	/**
	 * 生成验证码
	 *
	 * @param passengerPhone
	 * @return
	 */
	public ResponseResult generateVerificationCode(String passengerPhone) {
		ResponseResult<NumberCodeResponse> responseResult = serviceVerificationCodeClient.getNumberCode(6);
		int numberCode = responseResult.getData().getNumberCode();
		// 将验证码存入redis，并设置有效期2分钟
		String key = RedisPrefixUtils.generateKeyByPhone(passengerPhone,IdentityConstant.PASSENGER_IDENTITY);
		redisTemplate.opsForValue().set(key, String.valueOf(numberCode), 10, TimeUnit.MINUTES);
		return ResponseResult.success();
	}

	/**
	 * 验证码登录注册
	 * @param passengerPhone 电话
	 * @param verificationCode 验证码
	 * @return
	 */
	public ResponseResult checkVerificationCode(String passengerPhone, String verificationCode) {
		// 	根据手机号从redis查找验证码
		// 	生成key
		String key = RedisPrefixUtils.generateKeyByPhone(passengerPhone,IdentityConstant.PASSENGER_IDENTITY);
		// 	根据key获取value
		String codeRedis = stringRedisTemplate.opsForValue().get(key);
		// 	校验验证码
		if (StringUtils.isBlank(codeRedis)) {
			return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(), CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
		}
		if (!verificationCode.trim().equals(codeRedis.trim())) {
			return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(), CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
		}

		// 	判断原来是否有用户，并进行处理
		VerificationCodeDTO verificationCodeDTO = new VerificationCodeDTO();
		verificationCodeDTO.setPassengerPhone(passengerPhone);
		passengerUserClient.loginOrRegister(verificationCodeDTO);

		// 	颁发令牌
		String accessToken = JwtUtils.generateToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
		String refreshToken = JwtUtils.generateToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY,TokenConstants.REFRESH_TOKEN_TYPE);
		// 将token存到redis
		String accessTokenKey = RedisPrefixUtils.generateTokenKey(passengerPhone, IdentityConstant.PASSENGER_IDENTITY,TokenConstants.ACCESS_TOKEN_TYPE);
		stringRedisTemplate.opsForValue().set(accessTokenKey, accessToken, 15, TimeUnit.DAYS);
		String refreshTokenKey = RedisPrefixUtils.generateTokenKey(passengerPhone, IdentityConstant.PASSENGER_IDENTITY,TokenConstants.REFRESH_TOKEN_TYPE);
		stringRedisTemplate.opsForValue().set(refreshTokenKey, refreshToken, 16, TimeUnit.DAYS);

		// 响应
		TokenResponse tokenResponse = new TokenResponse();
		tokenResponse.setAccessToken(accessToken);
		tokenResponse.setRefreshToken(refreshToken);

		return ResponseResult.success(tokenResponse);
	}


}
